package org.jeecg.modules.zdh.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.until.Sm4Util;
import org.jeecg.modules.zdh.entity.Chry;
import org.jeecg.modules.zdh.service.IChryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 参会人员
 * @Author: jeecg-boot
 * @Date:   2023-12-11
 * @Version: V1.0
 */
@Slf4j
@Api(tags="参会人员")
@RestController
@RequestMapping("/zdh/chry")
public class ChryController extends JeecgController<Chry, IChryService> {
	@Autowired
	private IChryService chryService;

//	 @GetMapping(value = "/testHKWS")
//	 public String testHKWS() {
//		 // 最简单的HTTP请求，可以自动通过header等信息判断编码，不区分HTTP和HTTPS
//		 String result1= HttpUtil.get("https://www.baidu.com");
//		 return result1;
//	 }


//	 {"msg_id":"497834082441216"}
//	 https://api.sms.jpush.cn/v1/codes/{msg_id}/valid

//	 @GetMapping(value = "/testJPUSH2")
//	 public String testJPUSH2(String msg_id,String code) {
////		 HashMap<String,Object> paramMap = new HashMap<String,Object>();
////		 paramMap.put("mobile","18690214367");
////		 paramMap.put("sign_id","26675");
////		 paramMap.put("temp_id","1");
//		 JSONObject json = JSONUtil.createObj()
//				 .set("code",code);
//		 String jsonStr = JSONUtil.toJsonStr(json);
//		 String url = "https://api.sms.jpush.cn/v1/codes/"+msg_id+"/valid";
//		 String result2 = HttpRequest.post(url)
//				 .header("content-type","application/json")//头信息，多个头信息多次调用此方法即可
//				 .header("Authorization","Basic ZDgwYzEyNWFjOTdkYTE2OTJmZjA3YWI3OjZhOWZhMzNkZGFiZGJiYWM2ZWNkMTY0ZA==")//头信息，多个头信息多次调用此方法即可
//				 .body(jsonStr)
//				 .timeout(20000)//超时，毫秒
//				 .execute().body();
//		 return result2;
//	 }

	 @GetMapping(value = "/testSM4")
	 public String testSM4() {
//		 String key = "aaaabbbbccccdddd";
//	 	 String content = "刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光";
//		 SymmetricCrypto sm4 = SmUtil.sm4(key.getBytes());
////		 String result1= HttpUtil.get("https://www.baidu.com");
////		 String digestHex = SmUtil.sm3("aaaaa");
////		 String digestHex = SecureUtil.md5("aaaaaaaaa");
//		 String encryptHex = sm4.encryptHex(content);
//		 System.out.println(encryptHex);
//		 String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
//		 System.out.println(decryptStr);
//		 JSONObject obj = new JSONObject();
//		 obj.put("加密值",content);
//		 obj.put("解密值",decryptStr);

//		 String content = "刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光刘晨光";
//		 SymmetricCrypto sm4 = new SymmetricCrypto("SM4");
//
//		 String encryptHex = sm4.encryptHex(content);
//		 System.out.println(encryptHex);
//		 String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);//test中文
//		 System.out.println(decryptStr);

//		 String content = "{\"人员姓名\":\"刘晨光\",\"人员电话\":\"18690214367\"}";
//		 SymmetricCrypto sm4 = SmUtil.sm4(key.getBytes());
//
//		 String encryptHex = sm4.encryptHex(content);
//		 System.out.println(encryptHex);
//		 String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
//
//
//
		 return Sm4Util.SM4encrypt("");
	 }

	 @GetMapping(value = "/testSM3")
	 public String testSM3() {
		 String key = "aaaabbbbccccdddd";

		 String content = "刘晨光";
//		 SymmetricCrypto sm4 = SmUtil.sm4(key.getBytes());
//		 String result1= HttpUtil.get("https://www.baidu.com");
		 String digestHex = SmUtil.sm3("刘晨光");
//		 String digestHex = SecureUtil.md5("aaaaaaaaa");
//		 String encryptHex = sm4.encryptHex(content);
//		 String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
//		 JSONObject obj = new JSONObject();
//		 obj.put("加密值",content);
//		 obj.put("解密值",decryptStr);

		 return digestHex;
	 }

	/**
	 * 分页列表查询
	 *
	 * @param chry
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "参会人员-分页列表查询")
	@ApiOperation(value="参会人员-分页列表查询", notes="参会人员-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Chry chry,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Chry> queryWrapper = QueryGenerator.initQueryWrapper(chry, req.getParameterMap());
		Page<Chry> page = new Page<Chry>(pageNo, pageSize);
		IPage<Chry> pageList = chryService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param chry
	 * @return
	 */
	@AutoLog(value = "参会人员-添加")
	@ApiOperation(value="参会人员-添加", notes="参会人员-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Chry chry) {
		chryService.save(chry);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 参会人员加密
	 *
	 * @return
	 */
	@AutoLog(value = "参会人员-编辑")
	@ApiOperation(value="参会人员-编辑", notes="参会人员-编辑")
	@GetMapping(value = "/chryEncrypt")
	public Result<?> chryEncrypt() {
		List<Chry> chryList = chryService.list();
		for (Chry chry:chryList) {
			chry.set姓名(Sm4Util.SM4encrypt(chry.get姓名decrypt()));
			chry.set手机号(Sm4Util.SM4encrypt(chry.get手机号decrypt()));
			chry.set角色(Sm4Util.SM4encrypt(chry.get角色decrypt()));
			chry.set代表团(Sm4Util.SM4encrypt(chry.get代表团decrypt()));
			chry.set住宿(Sm4Util.SM4encrypt(chry.get住宿decrypt()));
			chryService.updateById(chry);
		}
		return Result.OK("编辑成功!");
	}


	 /**
	  * 编辑
	  *
	  * @param chry
	  * @return
	  */
	 @AutoLog(value = "参会人员-编辑")
	 @ApiOperation(value="参会人员-编辑", notes="参会人员-编辑")
	 @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<?> edit(@RequestBody Chry chry) {
		 chryService.updateById(chry);
		 return Result.OK("编辑成功!");
	 }

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "参会人员-通过id删除")
	@ApiOperation(value="参会人员-通过id删除", notes="参会人员-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		chryService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "参会人员-批量删除")
	@ApiOperation(value="参会人员-批量删除", notes="参会人员-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.chryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过加密手机号查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "参会人员-通过id查询")
	@ApiOperation(value="参会人员-通过id查询", notes="参会人员-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Chry chry = chryService.getById(id);
		return Result.OK(chry);
	}

	 @GetMapping(value = "/queryByPhone")
	 public Result<?> queryByPhone(@RequestParam(name="phone",required=true) String phone) {
		 QueryWrapper<Chry> queryWrapper = new QueryWrapper<Chry>();
		 queryWrapper.eq("手机号",phone);
		 Chry chry = chryService.getOne(queryWrapper,false);
		 return Result.OK(chry);
	 }

  /**
   * 导出excel
   *
   * @param request
   * @param chry
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, Chry chry) {
      return super.exportXls(request, chry, Chry.class, "参会人员");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, Chry.class);
  }

}
